package com.itheima.mm.controller;

import com.itheima.mm.dto.CatalogDTO;
import com.itheima.mm.entity.CatalogPageQueryParams;
import com.itheima.mm.entity.PageResult;
import com.itheima.mm.entity.QueryPageBean;
import com.itheima.mm.entity.Result;
import com.itheima.mm.service.CatalogService;
import com.itheima.mm.vo.CatalogVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.Map;


/**
 * @Description CatalogController
 * @Author yunchuan
 * @Date 2024-06-27
 */
@RestController
@Slf4j
@RequestMapping("/catalog")
public class CatalogController {
    @Autowired
    private CatalogService catalogService;

    //添加新学科目录
    @PostMapping("/add.do")
    public Result add(@RequestBody CatalogDTO catalogDTO) {
       log.info("开始执行新增学科目录接口：{}",catalogDTO);

        catalogService.add(catalogDTO);
        return Result.success(null,"学科目录添加成功");
    }

    //学科目录分页查询
    @PostMapping("/findListByPage.do")
    public Result<PageResult<CatalogVO>> findListByPage(@RequestBody QueryPageBean<CatalogPageQueryParams> queryPageBean){
        log.info("执行添加学科目录分页列表操作：{}",queryPageBean);

        PageResult pageResult = catalogService.findListByPage(queryPageBean);
        return Result.success(pageResult);
    }

    //学科目录删除
    @GetMapping("/delete.do")
    public Result delete(Integer catalogId) {
        log.info("执行删除学科目录操作：{}",catalogId);
        //调用service删除
        catalogService.delete(catalogId);
        //返回数据
        return Result.success(null,"学科目录删除成功");
    }

    //学科目录修改
    @PostMapping("/update.do")
    public Result update(@RequestBody CatalogDTO catalogDTO) {
        log.info("开始执行学科目录更新接口：{}",catalogDTO);
        //调用service方法实现更新
        catalogService.update(catalogDTO);
        return Result.success(null,"学科目录更新成功");
    }
    //学科目录禁用
    @PostMapping("/disable")
    public ResponseEntity<String> disableCatalog(@RequestBody Map<String, Object> params) {
        Integer id = (Integer) params.get("id");
        if (id == null) {
            return ResponseEntity.badRequest().body("无效的目录ID");
        }
        boolean result = catalogService.disableCatalog(id);
        return result ? ResponseEntity.ok("目录禁用成功") : ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("目录禁用失败");
    }
    //学科目录启用
    @PostMapping("/enable")
    public ResponseEntity<String> enableCatalog(@RequestBody Map<String, Object> params) {
        Integer id = (Integer) params.get("id");
        if (id == null) {
            return ResponseEntity.badRequest().body("无效的目录ID");
        }
        boolean result = catalogService.enableCatalog(id);
        return result ? ResponseEntity.ok("目录启用成功") : ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("目录启用失败");
    }
}
